﻿package com.tudou.model
{

    public class MetaData extends Object
    {
        private var videoHeight:Number = 300;
        private var _isMP4:Boolean = false;
        private var videoWidth:Number = 400;
        private var duration:Number = 0;
        private var fileSize:Number = 0;
        private var keyFrames:Array;
        private var data:Object;
        private static const VIDEO_HEIGHT_TAG_NAME:String = "height";
        private static const FILE_SIZE_TAG_NAME:String = "filesize";
        private static const VIDEO_WIDTH_TAG_NAME:String = "width";
        private static const VIDEO_DURATION_TAG_NAME:String = "duration";
        private static const MP4_KEYFRAMES_TAG_NAME:String = "seekpoints";
        private static const FLV_KEYFRAMES_TAG_NAME:String = "keyframes";

        public function MetaData(param1:Object) : void
        {
            keyFrames = new Array();
            if (param1 == null)
            {
                trace("MetaData is NULL!");
                return;
            }
            this.data = param1;
            if (data.hasOwnProperty(VIDEO_WIDTH_TAG_NAME))
            {
                videoWidth = Number(data[VIDEO_WIDTH_TAG_NAME]);
            }
            if (data.hasOwnProperty(VIDEO_HEIGHT_TAG_NAME))
            {
                videoHeight = Number(data[VIDEO_HEIGHT_TAG_NAME]);
            }
            if (data.hasOwnProperty(VIDEO_DURATION_TAG_NAME))
            {
                duration = Number(data[VIDEO_DURATION_TAG_NAME]);
            }
            if (data.hasOwnProperty(FILE_SIZE_TAG_NAME))
            {
                fileSize = Math.max(Number(data[FILE_SIZE_TAG_NAME]), 0);
            }
            parseKeyFrames();
            return;
        }// end function

        public function getIndexByTime(param1:Number, param2:Number = 0) : int
        {
            if (!hasKeyFrames)
            {
                return 0;
            }
            var _loc_3:* = findKeyframe("time", param1, 0, (keyFrames.length - 1));
            var _loc_4:* = _loc_3;
            var _loc_5:* = keyFrames[_loc_3];
            var _loc_6:* = keyFrames[_loc_4];
            if (param2 > 0)
            {
                while (_loc_4 < (keyframesLength - 1) && _loc_6.time - _loc_5.time < param2)
                {
                    
                    _loc_4++;
                    _loc_6 = keyFrames[_loc_4];
                }
            }
            else if (param2 < 0)
            {
                while (_loc_4 > 0 && _loc_6.time - _loc_5.time > param2)
                {
                    
                    _loc_4 = _loc_4 - 1;
                    _loc_6 = keyFrames[_loc_4];
                }
            }
            _loc_3 = _loc_4;
            return _loc_3;
        }// end function

        private function get orgKeyframes() : Object
        {
            if (data && data[FLV_KEYFRAMES_TAG_NAME])
            {
                return Object(data[FLV_KEYFRAMES_TAG_NAME]);
            }
            return null;
        }// end function

        public function getKeyPointByByte(param1:Number) : KeyInfo
        {
            var _loc_2:* = getIndexByByte(param1);
            if (keyFrames[_loc_2] == null)
            {
                return new KeyInfo(0, 0);
            }
            return keyFrames[_loc_2];
        }// end function

        private function findKeyframe(param1:String, param2:Number, param3:int, param4:int) : int
        {
            if (!param2)
            {
                return 0;
            }
            if (param4 == param3)
            {
                return param4;
            }
            if (param4 - param3 == 1)
            {
                if (param2 >= keyFrames[param4][param1])
                {
                    return param4;
                }
                return param3;
            }
            var _loc_5:* = Math.ceil((param3 + param4) / 2);
            var _loc_6:* = keyFrames[_loc_5];
            var _loc_7:* = keyFrames[_loc_5][param1];
            if (param2 < _loc_7)
            {
                return findKeyframe(param1, param2, param3, (_loc_5 - 1));
            }
            if (param2 > _loc_7)
            {
                return findKeyframe(param1, param2, _loc_5, param4);
            }
            return _loc_5;
        }// end function

        public function getKeyPointByTime(param1:Number, param2:Number = 0) : KeyInfo
        {
            var _loc_3:* = getIndexByTime(param1, param2);
            if (keyFrames[_loc_3] == null)
            {
                return new KeyInfo(0, 0);
            }
            return keyFrames[_loc_3];
        }// end function

        public function get width() : Number
        {
            return videoWidth;
        }// end function

        public function get isMP4() : Boolean
        {
            return _isMP4;
        }// end function

        public function get keyframesLength() : uint
        {
            return keyFrames ? (keyFrames.length) : (0);
        }// end function

        public function get height() : Number
        {
            return videoHeight;
        }// end function

        public function get hasKeyFrames() : Boolean
        {
            return keyFrames && keyFrames.length > 0;
        }// end function

        public function get totalSize() : Number
        {
            return fileSize;
        }// end function

        public function get totalTime() : Number
        {
            return duration;
        }// end function

        private function parseKeyFrames() : void
        {
            var _loc_3:KeyInfo = null;
            var _loc_6:Array = null;
            var _loc_7:Array = null;
            var _loc_8:uint = 0;
            var _loc_9:uint = 0;
            var _loc_1:* = orgKeyframes;
            var _loc_2:* = orgSeekPoints;
            var _loc_4:Number = -1;
            var _loc_5:Number = -1;
            if (_loc_1)
            {
                _loc_6 = _loc_1["filepositions"] as Array;
                _loc_7 = _loc_1["times"] as Array;
                _loc_8 = 0;
                while (_loc_8 < _loc_6.length)
                {
                    
                    _loc_3 = new KeyInfo();
                    _loc_3.size = _loc_6[_loc_8];
                    _loc_3.time = _loc_7[_loc_8];
                    if (_loc_3.size <= 0 || _loc_3.time < 0)
                    {
                    }
                    else if (_loc_3.size <= _loc_5 || _loc_3.time <= _loc_4)
                    {
                    }
                    else if (_loc_3.size >= totalSize || _loc_3.time >= totalTime)
                    {
                    }
                    else
                    {
                        _loc_5 = _loc_3.size;
                        _loc_4 = _loc_3.time;
                        keyFrames.push(_loc_3);
                    }
                    _loc_8 = _loc_8 + 1;
                }
            }
            else if (_loc_2)
            {
                _isMP4 = true;
                _loc_9 = 0;
                while (_loc_9 < _loc_2.length)
                {
                    
                    _loc_3 = new KeyInfo();
                    _loc_3.size = _loc_2[_loc_9]["offset"];
                    _loc_3.time = _loc_2[_loc_9]["time"];
                    if (_loc_3.size <= 0 || _loc_3.time < 0)
                    {
                    }
                    else if (_loc_3.size <= _loc_5 || _loc_3.time <= _loc_4)
                    {
                    }
                    else if (_loc_3.size >= totalSize || _loc_3.time >= totalTime)
                    {
                    }
                    else
                    {
                        _loc_5 = _loc_3.size;
                        _loc_4 = _loc_3.time;
                        keyFrames.push(_loc_3);
                    }
                    _loc_9 = _loc_9 + 1;
                }
            }
            trace("over!");
            return;
        }// end function

        public function getKeyInfo(param1:int) : KeyInfo
        {
            var _loc_2:KeyInfo = null;
            if (!hasKeyFrames || param1 < 0)
            {
                _loc_2 = new KeyInfo();
            }
            else if (param1 > (keyFrames.length - 1))
            {
                _loc_2 = keyFrames[(keyFrames.length - 1)];
            }
            else
            {
                _loc_2 = keyFrames[param1];
            }
            return _loc_2;
        }// end function

        public function getIndexByByte(param1:Number) : int
        {
            if (keyFrames == null || keyFrames.length == 0)
            {
                return 0;
            }
            var _loc_2:* = findKeyframe("size", param1, 0, (keyFrames.length - 1));
            return _loc_2;
        }// end function

        private function get orgSeekPoints() : Array
        {
            if (data && data[MP4_KEYFRAMES_TAG_NAME])
            {
                return data[MP4_KEYFRAMES_TAG_NAME] as Array;
            }
            return null;
        }// end function

    }
}
